package cn.flashk.controls.skin.sourceSkin {
	import cn.flashk.controls.TextInput;
	import cn.flashk.controls.support.UIComponent;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;

	public class TextInputSourceSkin extends SourceSkin {
		private static var bd:BitmapData;
		private static var skin:DisplayObject;
		private static var bds:Array;
		private var tar:TextInput;
		private var focusIn:Boolean = false;

		public function TextInputSourceSkin(skinID:String = ""){
			super(skinID);
		}

		override public function init(target:UIComponent, styleSet:Object, Skin:Class):void {
			super.init(target, styleSet, Skin);
			tar = target as TextInput;
			if (Skin == null){
				bds = [];
				drawImgLoader(tar, bds);
				tar.addChildAt(bds[0], 0);
				tar.addEventListener(MouseEvent.MOUSE_OVER, skinOver);
				tar.addEventListener(MouseEvent.MOUSE_OUT, skinOut);
			} else {
				if (skin == null){
					skin = new Skin() as DisplayObject;
				}
				initBp(skin);
				if (bds == null){
					bds = new Array();
					drawMovieClipToArray(skin as MovieClip, bds);
				}
				tar.addChildAt(bp, 0);
				tar.addEventListener(MouseEvent.MOUSE_OVER, showOver);
				tar.addEventListener(MouseEvent.MOUSE_OUT, showOut);
				//tar.addEventListener(Event.INIT,iniFocusLis);
				bp.sourceBitmapData = bds[1] as BitmapData;
			}
			tar.addEventListener(Event.INIT, skinHandler);
		}

		protected function skinOut(e:MouseEvent):void {
			if (focusIn == true)
				return;
			tar.removeChildAt(0);
			tar.addChildAt(bds[0], 0)
		}

		protected function skinOver(e:MouseEvent):void {
			if (focusIn == true)
				return;
			tar.removeChildAt(0);
			tar.addChildAt(bds[1], 0)
		}

		protected function skinHandler(e:Event):void {
			tar.textField.addEventListener(FocusEvent.FOCUS_IN, skinFocusIn);
			tar.textField.addEventListener(FocusEvent.FOCUS_OUT, skinFocusOut);
		}

		private function skinFocusIn(event:Event):void {
			focusIn = true;
			if (_skinID == ""){
				bp.sourceBitmapData = bds[1] as BitmapData;
				bp.update();
			} else {
				tar.removeChildAt(0);
				tar.addChildAt(bds[2], 0)
			}
		}

		private function skinFocusOut(event:Event):void {
			focusIn = false;
			if (_skinID == ""){
				bp.sourceBitmapData = bds[3] as BitmapData;
				bp.update();
			} else {
				tar.removeChildAt(0);
				tar.addChildAt(bds[0], 0)
			}
		}

		protected function iniFocusLis(event:Event):void {
			tar.textField.addEventListener(FocusEvent.FOCUS_IN, setFocusIn);
			tar.textField.addEventListener(FocusEvent.FOCUS_OUT, setFocusOut);
		}

		protected function showOver(event:MouseEvent):void {
			if (focusIn == true)
				return;
			bp.sourceBitmapData = bds[2] as BitmapData;
			bp.update();
		}

		protected function showOut(event:MouseEvent):void {
			if (focusIn == true)
				return;
			bp.sourceBitmapData = bds[1] as BitmapData;
			bp.update();
		}

		override public function reDraw():void {
			if (_skinID != ""){
				for each (var item:DisplayObject in bds){
					item.width = tar.compoWidth;
					item.height = tar.compoHeight;
				}
				return;
			}
			bp.width = tar.compoWidth + sx * 2;
			bp.height = tar.compoHeight + sy * 2;
		}

		private function setFocusIn(event:Event):void {
			focusIn = true;
			bp.sourceBitmapData = bds[3] as BitmapData;
			bp.update();
		}

		private function setFocusOut(event:Event):void {
			focusIn = false;
			bp.sourceBitmapData = bds[1] as BitmapData;
			bp.update();
		}
	}
}